<?php

namespace app\admin\model;

use think\Loader;
use think\Model;
use think\Session;

class Admin extends Model
{
    protected $pk = 'admin_id';
    protected $table = 'admin';

    public function login($data)
    {
        //1.执行验证
        $validate = Loader::validate('Admin');

        if (!$validate->check($data)) {
            //
            return ['valid' => 0, 'msg' => $validate->getError()];
        }
        //2.对比用户名是否正确
        $res = $this->where('admin_username', $data['admin_username'])->where('admin_password', md5(md5($data['admin_password'])))->find();
        if (!$res) {
            return ['valid' => 0, 'msg' => '用户名或密码不正确,请重新输入'];
        }
        //3.将用户的信息存入到session中
        Session::set('name', $data['admin_username']);
        return ['valid' => 1, 'msg' => '恭喜您,登录成功'];
    }

}
